package com.ca.project.Server.Modules.RotatingChat.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ca.project.Server.Modules.RotatingChat.Context.UploadRotatingChatContext;
import com.ca.project.Server.Modules.RotatingChat.Converter.RotatingChatConverter;
import com.ca.project.Server.Modules.RotatingChat.Entity.Rotatingchat;
import com.ca.project.Server.Modules.RotatingChat.PO.UploadRotatingChatPO;
import com.ca.project.Server.Modules.RotatingChat.Service.RotatingChatService;
import com.ca.project.Server.Modules.RotatingChat.VO.DeleteRotatingChatVO;
import com.ca.project.Server.Modules.RotatingChat.VO.DisableRotatingChatVO;
import com.ca.project.Server.Modules.RotatingChat.VO.RecoverRotatingChatVO;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.RotatingChat.Controller
 * @NAME: RotatingChatController
 * @USER: ASUS
 * @DATE: 31/12/2024
 * @TIME: 下午2:44
 **/
@RestController
@RequestMapping("/rotating")
@Api(tags = "轮播图模块")
public class RotatingChatController {

    @Autowired
    private RotatingChatService rotatingchatService;
    @Autowired
    private RotatingChatConverter rotatingChatConverter;

    @ApiOperation(
            value = "轮播图上传接口",
            notes = "该接口提供了轮播图上传的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/upload")
    public R uploadRotatingChat(@Validated UploadRotatingChatPO uploadRotatingChatPO) {
        UploadRotatingChatContext uploadRotatingChatContext =
                rotatingChatConverter.UploadRotatingChatPO2UploadRotatingChatContext(uploadRotatingChatPO);
        rotatingchatService.uploadRotatingChat(uploadRotatingChatContext);
        return R.success();
    }

    @ApiOperation(
            value = "轮播图停用接口",
            notes = "该接口提供了轮播图停用的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/disable")
    public R disableRotatingChat(@Validated @RequestBody DisableRotatingChatVO disableRotatingChatVO) {
        Long id = disableRotatingChatVO.getId();
        rotatingchatService.disable(id);
        return R.success();
    }

    @ApiOperation(
            value = "轮播图恢复接口",
            notes = "该接口提供了轮播图恢复的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/recover")
    public R RecoverRotatingChat(@Validated @RequestBody RecoverRotatingChatVO recoverRotatingChatVO) {
        Long id = recoverRotatingChatVO.getId();
        rotatingchatService.recover(id);
        return R.success();
    }

    @ApiOperation(
            value = "查询所有轮播图信息-分页",
            notes = "该接口提供了查询所有轮播图信息-分页的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/{currentPage}/{PageSize}")
    public R GetRotatingChat(@PathVariable Integer currentPage, @PathVariable Integer PageSize, Rotatingchat rotatingchat) {
        IPage<Rotatingchat> page = rotatingchatService.getPage(currentPage, PageSize, rotatingchat);
        //如果当前页码之大于总页码值，重新执行查询操作，使用最大页码作为当前页码值
        if (currentPage > page.getPages())//如果当前页码之大于总页码值
            page = rotatingchatService.getPage((int) page.getPages(), PageSize);
        return R.data(page);
    }

    @ApiOperation(
            value = "轮播图删除接口",
            notes = "该接口提供了轮播图删除的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/delete")
    public R deleteRotatingChat(@Validated @RequestBody DeleteRotatingChatVO deleteRotatingChatVO) {
        String filename = deleteRotatingChatVO.getFilename();
        boolean success = rotatingchatService.deleteRotatingChat(filename);
        return R.data(success);
    }

}
